<template>
  <div class="blog-background"
      style="/*background-color: #F2F4F4;*/width: 100vw;height: 100vh;display: flex;justify-content: center;align-items: center">
    <div style="position: absolute;left: 50px;top: 30px;color:#922B21 ">
      <h1>个人博客</h1>

    </div>
    <div style="width: 25%;height: 60%;margin-right: 10px">
        <img src="https://iconfont.alicdn.com/t/212918b0-a205-44d5-a573-ae159e03b513.png"
             style="width: 100%;height: 100%">
    </div>
    <div style="width: 25%;height: 60%;">
      <el-card style="width: 100%;height: 100%">

        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm" label-position="top">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="ruleForm.password" placeholder="请输入密码"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" round style="width: 100%" @click="submitForm('ruleForm')">登录</el-button>
          </el-form-item>
          <el-form-item>
            <el-button round style="width: 100%" @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>

    </div>

  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      ruleForm: {
        username: 'admin',
        password: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请选择密码', trigger: 'change'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const _this = this
          this.$axios.post('/login', this.ruleForm).then(res => {

            // console.log(res.data)
            const jwt = res.headers['authorization']
            const userInfo = res.data.data

            // 把数据共享出去
            _this.$store.commit("SET_TOKEN", jwt)
            _this.$store.commit("SET_USERINFO", userInfo)

            // 获取
            // console.log(_this.$store.getters.getUser)

            this.$notify({
              type: "success",
              title: '登录信息',
              message: '成功，giao~',
              duration: 3000
            });

            _this.$router.push("/blogs")
          })

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.el-header, .el-footer {
  background-color: #242424;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #D3DCE6;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  /*background-color: #E9EEF3;*/
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

.mlogo {
  height: 100%;
  margin-top: 0;
}

.demo-ruleForm {
  max-width: 500px;
  margin: 0 auto;
}

</style>